package team.hyznrj.studentsys.utils;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import team.hyznrj.studentsys.entity.ExcelEntity;
import team.hyznrj.studentsys.entity.ExcelHeader;
import team.hyznrj.studentsys.entity.ExcelUnitObject;
import team.hyznrj.studentsys.exception.DataIllegalException;
import team.hyznrj.studentsys.exception.ExcelIndexOutOfRangeException;
import team.hyznrj.studentsys.exception.NullExcelHeaderException;
public class QualityTableCreator {
	public static String QUALITY_HEADER_STR="学号@奖励分总分@扣除分总分@科技创新@技术技能总分@组织管理分@特殊分";
	public static String[]QUALITY_TYPE={"虑空(从index=1开始)","思想政治分","思想政治分","科技创新分",
			"技术技能分","组织管理分","特殊加分"};
	public static String[] QUALITY_CREATE_HEADER_STR={"学号","加分项目","加分值","加分类型","学年","学期"};
	public static DecimalFormat FORMAT=new DecimalFormat("0.00");
	/**
	 * 将横向表写为模版表
	 * @throws IOException 
	 * @throws ExcelIndexOutOfRangeException 
	 * @throws NullExcelHeaderException 
	 * @throws DataIllegalException 
	 */
	public static void WriteQualityByRead(String input,String output,String year,String item) throws DataIllegalException, NullExcelHeaderException, ExcelIndexOutOfRangeException, IOException{
		ExcelHeader head = new ExcelHeader();
		// 学号@课程名称@学分@备注@折算成绩@绩点@课程性质@学年@学期
		String[] split = QUALITY_HEADER_STR.split("@");
		Map<String, Integer> location = new HashMap<>();
		for (int i = 0; i < split.length; i++) {
			System.out.println("header:"+split[i]);
			String str = split[i];
			head.append(str, true);
			location.put(str, i);
		}
		/**
		 * 获取数据类型
		 */
		List<ArrayList<String>> list = new ReadExcel().readWorkSheet(input, head, 0).getList();
		List<QualityModel> form = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
			ArrayList<String> header = head.getHeader();
			ArrayList<String> colValue = list.get(i);
			int ord = 0;
			String sno = ValueUtil.noneEmpty(colValue.get(location.get(header.get(ord))), null, false);
			if(sno==null){
				continue;
			}
			for(int k=1;k<split.length;k++){
				QualityModel model=new QualityModel();
				model.setSno(sno);
				String tmp = ValueUtil.noneEmpty(colValue.get(location.get(header.get(k))), null, false);
				if(tmp==null){
					continue;
				}
				System.out.println("value="+tmp);
				model.setProject(split[k]);
				model.setType(QUALITY_TYPE[k]);
				model.setValue(Double.valueOf(tmp));
				model.setAcademicYear(year);
				model.setItem(item);
				form.add(model);
			}
		}
		
		ExcelEntity entity = new ExcelEntity(new Date().toString());
		entity.setSheetName("sheet1");
//		QUALITY_CREATE_HEADER_STR
		String value=null;
		/**写表头*/
		for(int i=0;i<QUALITY_CREATE_HEADER_STR.length;i++){
			value=QUALITY_CREATE_HEADER_STR[i];
			ExcelUnitObject obj = new ExcelUnitObject(i, 0, 0, 0, value, false);
			entity.addContent(obj);
		}
		int locationY=1;
		for(int i=0;i<form.size();i++){
			
			QualityModel qualityModel = form.get(i);
			if(qualityModel.value==0){
				continue;
			}
			//{"学号","加分项目","加分值","加分类型","学年","学期"};
			//学号
			value=qualityModel.sno;
			ExcelUnitObject obj = new ExcelUnitObject(0, locationY, 0, 0, value, false);
			entity.addContent(obj);
			//加分项目
			value=qualityModel.project;
			obj = new ExcelUnitObject(1, locationY, 0, 0, value, false);
			entity.addContent(obj);
			//加分值
			value=FORMAT.format(qualityModel.value);
			obj = new ExcelUnitObject(2, locationY, 0, 0, value, false);
			entity.addContent(obj);
			//加分类型
			value=qualityModel.type;
			 obj = new ExcelUnitObject(3,locationY, 0, 0, value, false);
			entity.addContent(obj);
			//学年
			value=qualityModel.academicYear;
			 obj = new ExcelUnitObject(4, locationY, 0, 0, value, false);
			entity.addContent(obj);
			//学期
			value=qualityModel.item;
			 obj = new ExcelUnitObject(5, locationY, 0, 0, value, false);
			entity.addContent(obj);
			locationY++;
		}
		new WriteExcel().createExccel(entity, output);
	}
}


class QualityModel{
	String sno;
	String project;
	double value;
	String type;
	String academicYear;
	String item;
	public String getSno() {
		return sno;
	}
	public void setSno(String sno) {
		this.sno = sno;
	}
	public String getProject() {
		return project;
	}
	public void setProject(String project) {
		this.project = project;
	}
	public double getValue() {
		return value;
	}
	public void setValue(double value) {
		this.value = value;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getAcademicYear() {
		return academicYear;
	}
	public void setAcademicYear(String academicYear) {
		this.academicYear = academicYear;
	}
	public String getItem() {
		return item;
	}
	public void setItem(String item) {
		this.item = item;
	}
	
}
